package com.fr.data.condition;

import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.base.core.ComparatorUtils;
import com.fr.data.core.Compare;
import com.fr.report.CellElement;
import com.fr.report.Report;
import com.fr.report.core.ScriptUtils;
import com.fr.report.parameter.Parameter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LiteConditionUtils
{
  public static Condition ColumnRowToValue(Condition paramCondition, Report paramReport)
  {
    try
    {
      Object localObject1;
      Object localObject3;
      if (paramCondition instanceof CommonCondition)
      {
        localObject1 = (CommonCondition)paramCondition.clone();
        Compare localCompare = ((CommonCondition)localObject1).getCompare();
        Object localObject2 = localCompare.getValue();
        if ((localObject2 != null) && (localObject2 instanceof ColumnRow))
        {
          localObject3 = (ColumnRow)localObject2;
          CellElement localCellElement = paramReport.getCellElement(((ColumnRow)localObject3).column, ((ColumnRow)localObject3).row);
          if (localCellElement == null)
            localCompare.setValue(null);
          else
            localCompare.setValue(localCellElement.getValue());
        }
        return localObject1;
      }
      if (paramCondition instanceof FormulaCondition)
        return ((FormulaCondition)paramCondition.clone());
      if (paramCondition instanceof ListCondition)
      {
        localObject1 = (ListCondition)paramCondition.clone();
        int i = ((ListCondition)localObject1).getJoinConditionCount();
        for (int j = 0; j < i; ++j)
        {
          localObject3 = ((ListCondition)localObject1).getJoinCondition(j);
          ((JoinCondition)localObject3).setCondition(ColumnRowToValue(((JoinCondition)localObject3).getCondition(), paramReport));
        }
        return localObject1;
      }
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    return ((Condition)(Condition)new ListCondition());
  }

  public static void getAllParameters(Condition paramCondition, List paramList)
  {
    Object localObject1;
    Object localObject2;
    int j;
    if (paramCondition instanceof CommonCondition)
    {
      localObject1 = ((CommonCondition)paramCondition).getCompare();
      localObject2 = (localObject1 == null) ? null : ((Compare)localObject1).getValue();
      if (localObject2 instanceof Parameter)
        paramList.add(localObject2);
    }
    else if (paramCondition instanceof ObjectCondition)
    {
      localObject1 = ((ObjectCondition)paramCondition).getCompare();
      localObject2 = (localObject1 == null) ? null : ((Compare)localObject1).getValue();
      if (localObject2 instanceof Parameter)
        paramList.add(localObject2);
    }
    else if (paramCondition instanceof ListCondition)
    {
      localObject1 = (ListCondition)paramCondition;
      int i = ((ListCondition)localObject1).getJoinConditionCount();
      for (j = 0; j < i; ++j)
        getAllParameters(((ListCondition)localObject1).getJoinCondition(j).getCondition(), paramList);
    }
  }

  public static void getAllLiteCondition(Condition paramCondition, List paramList)
  {
    if (paramCondition == null)
      return;
    if (paramCondition instanceof ListCondition)
    {
      ListCondition localListCondition = (ListCondition)paramCondition;
      int i = localListCondition.getJoinConditionCount();
      int j = 0;
      while (true)
      {
        if (j >= i)
          return;
        getAllLiteCondition(localListCondition.getJoinCondition(j).getCondition(), paramList);
        ++j;
      }
    }
    paramList.add(paramCondition);
  }

  public static void getAllJoinCondition(Condition paramCondition, List paramList)
  {
    Object localObject;
    int j;
    if (paramCondition == null)
      return;
    if (paramCondition instanceof CommonCondition)
    {
      localObject = new JoinCondition(0, paramCondition);
      paramList.add(localObject);
    }
    else if (paramCondition instanceof ListCondition)
    {
      localObject = (ListCondition)paramCondition;
      int i = ((ListCondition)localObject).getJoinConditionCount();
      for (j = 0; j < i; ++j)
      {
        Condition localCondition = ((ListCondition)localObject).getJoinCondition(j).getCondition();
        if (localCondition instanceof CommonCondition)
          paramList.add(((ListCondition)localObject).getJoinCondition(j));
      }
    }
  }

  public static int replaceLiteCondition(Condition paramCondition, String paramString1, String paramString2)
  {
    int k;
    int i = 0;
    if (paramCondition instanceof CommonCondition)
    {
      String str1 = ((CommonCondition)paramCondition).getColumnName();
      if (ComparatorUtils.equals(str1, paramString1))
      {
        str1 = paramString2;
        ((CommonCondition)paramCondition).setColumnName(str1);
        ++i;
      }
    }
    else if (paramCondition instanceof ListCondition)
    {
      int j = ((ListCondition)paramCondition).getJoinConditionCount();
      for (k = 0; k < j; ++k)
      {
        JoinCondition localJoinCondition = ((ListCondition)paramCondition).getJoinCondition(k);
        Condition localCondition = localJoinCondition.getCondition();
        if (localCondition instanceof CommonCondition)
        {
          String str2 = ((CommonCondition)localCondition).getColumnName();
          if (ComparatorUtils.equals(str2, paramString1))
          {
            str2 = paramString2;
            ((CommonCondition)localCondition).setColumnName(str2);
            localJoinCondition.setCondition(localCondition);
            ((ListCondition)paramCondition).setJoinCondition(k, localJoinCondition);
            ++i;
          }
        }
      }
    }
    return i;
  }

  public static void convertLiteConditionWhenInsertRow(Condition paramCondition, int paramInt)
  {
    Object localObject1;
    Object localObject2;
    int i;
    if (paramCondition instanceof CommonCondition)
    {
      localObject1 = (CommonCondition)paramCondition;
      if ((((CommonCondition)localObject1).getCompare() != null) && (((CommonCondition)localObject1).getCompare().getValue() instanceof ColumnRow))
      {
        localObject2 = (ColumnRow)((CommonCondition)localObject1).getCompare().getValue();
        if ((localObject2 != null) && (((ColumnRow)localObject2).row >= paramInt))
          ((CommonCondition)localObject1).getCompare().setValue(ColumnRow.valueOf(((ColumnRow)localObject2).column, ((ColumnRow)localObject2).row + 1));
      }
    }
    else if (paramCondition instanceof FormulaCondition)
    {
      localObject1 = ((FormulaCondition)paramCondition).getFormula();
      ((FormulaCondition)paramCondition).setFormula(ScriptUtils.moveRow((String)localObject1, paramInt, 1));
    }
    else if (paramCondition instanceof ListCondition)
    {
      localObject1 = new ArrayList();
      getAllLiteCondition(paramCondition, (List)localObject1);
      for (i = 0; i < ((List)localObject1).size(); ++i)
      {
        localObject2 = (Condition)((List)localObject1).get(i);
        convertLiteConditionWhenInsertRow((Condition)localObject2, paramInt);
      }
    }
  }

  public static void convertLiteConditionWhenRemoveRow(Condition paramCondition, int paramInt)
  {
    Object localObject1;
    Object localObject2;
    int i;
    if (paramCondition instanceof CommonCondition)
    {
      localObject1 = (CommonCondition)paramCondition;
      if ((((CommonCondition)localObject1).getCompare() != null) && (((CommonCondition)localObject1).getCompare().getValue() instanceof ColumnRow))
      {
        localObject2 = (ColumnRow)((CommonCondition)localObject1).getCompare().getValue();
        if ((localObject2 != null) && (((ColumnRow)localObject2).row >= paramInt))
          ((CommonCondition)localObject1).getCompare().setValue(ColumnRow.valueOf(((ColumnRow)localObject2).column, ((ColumnRow)localObject2).row - 1));
      }
    }
    else if (paramCondition instanceof FormulaCondition)
    {
      localObject1 = ((FormulaCondition)paramCondition).getFormula();
      ((FormulaCondition)paramCondition).setFormula(ScriptUtils.moveRow((String)localObject1, paramInt, -1));
    }
    else if (paramCondition instanceof ListCondition)
    {
      localObject1 = new ArrayList();
      getAllLiteCondition(paramCondition, (List)localObject1);
      for (i = 0; i < ((List)localObject1).size(); ++i)
      {
        localObject2 = (Condition)((List)localObject1).get(i);
        convertLiteConditionWhenRemoveRow((Condition)localObject2, paramInt);
      }
    }
  }

  public static void convertLiteConditionWhenInsertCol(Condition paramCondition, int paramInt)
  {
    Object localObject1;
    Object localObject2;
    int i;
    if (paramCondition instanceof CommonCondition)
    {
      localObject1 = (CommonCondition)paramCondition;
      if ((((CommonCondition)localObject1).getCompare() != null) && (((CommonCondition)localObject1).getCompare().getValue() instanceof ColumnRow))
      {
        localObject2 = (ColumnRow)((CommonCondition)localObject1).getCompare().getValue();
        if ((localObject2 != null) && (((ColumnRow)localObject2).column >= paramInt))
          ((CommonCondition)localObject1).getCompare().setValue(ColumnRow.valueOf(((ColumnRow)localObject2).column + 1, ((ColumnRow)localObject2).row));
      }
    }
    else if (paramCondition instanceof FormulaCondition)
    {
      localObject1 = ((FormulaCondition)paramCondition).getFormula();
      ((FormulaCondition)paramCondition).setFormula(ScriptUtils.moveColumn((String)localObject1, paramInt, 1));
    }
    else if (paramCondition instanceof ListCondition)
    {
      localObject1 = new ArrayList();
      getAllLiteCondition(paramCondition, (List)localObject1);
      for (i = 0; i < ((List)localObject1).size(); ++i)
      {
        localObject2 = (Condition)((List)localObject1).get(i);
        convertLiteConditionWhenInsertCol((Condition)localObject2, paramInt);
      }
    }
  }

  public static void convertLiteConditionWhenRemoveCol(Condition paramCondition, int paramInt)
  {
    Object localObject1;
    Object localObject2;
    int i;
    if (paramCondition instanceof CommonCondition)
    {
      localObject1 = (CommonCondition)paramCondition;
      if ((((CommonCondition)localObject1).getCompare() != null) && (((CommonCondition)localObject1).getCompare().getValue() instanceof ColumnRow))
      {
        localObject2 = (ColumnRow)((CommonCondition)localObject1).getCompare().getValue();
        if ((localObject2 != null) && (((ColumnRow)localObject2).column >= paramInt))
          ((CommonCondition)localObject1).getCompare().setValue(ColumnRow.valueOf(((ColumnRow)localObject2).column - 1, ((ColumnRow)localObject2).row));
      }
    }
    else if (paramCondition instanceof FormulaCondition)
    {
      localObject1 = ((FormulaCondition)paramCondition).getFormula();
      ((FormulaCondition)paramCondition).setFormula(ScriptUtils.moveColumn((String)localObject1, paramInt, -1));
    }
    else if (paramCondition instanceof ListCondition)
    {
      localObject1 = new ArrayList();
      getAllLiteCondition(paramCondition, (List)localObject1);
      for (i = 0; i < ((List)localObject1).size(); ++i)
      {
        localObject2 = (Condition)((List)localObject1).get(i);
        convertLiteConditionWhenRemoveCol((Condition)localObject2, paramInt);
      }
    }
  }
}